package br.edu.ufcg.greengrid.report;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;

import br.edu.ufcg.greengrid.event.Event;
import br.edu.ufcg.greengrid.model.Simulator;
import br.edu.ufcg.greengrid.util.OutputLazyList;
import br.edu.ufcg.greengrid.util.Transform;

import com.mallardsoft.tuple.Quadruple;
import com.mallardsoft.tuple.Tuple;

public class SummaryReporter extends AbstractReporter<Quadruple<Long, Integer, Integer, Integer>> {

	// <clock> <#machinesNotBeingUsedByUser> <gridDemand = waiting-jobs + running-jobs> <numJobsArrivedInLastTurn>

	private static final String REPORT_NAME = "summary";

	private static Transform<Quadruple<Long, Integer, Integer, Integer>> MY_TRANSFORM = new Transform<Quadruple<Long, Integer, Integer, Integer>>() {

		@Override
		public Quadruple<Long, Integer, Integer, Integer> from(String secondaryType) throws ParseException {

			String[] split = secondaryType.split(Event.SEPARATOR);
			int index = 0;
			return Tuple.from(Long.parseLong(split[index++]), Integer.parseInt(split[index++]), Integer
					.parseInt(split[index++]), Integer.parseInt(split[index++]));
		}

		@Override
		public String to(Quadruple<Long, Integer, Integer, Integer> mainType) throws ParseException {

			return mainType.toString("", Event.SEPARATOR, "");
		}

	};

	public SummaryReporter(long randomValue) {
		
		super(randomValue, new OutputLazyList<Quadruple<Long, Integer, Integer, Integer>>(new File(getOutputName(
				REPORT_NAME, randomValue)), MY_TRANSFORM));
	}

	@Override
	public void handleNewTimestamp(Simulator s) {

		Quadruple<Long, Integer, Integer, Integer> quadruple = Tuple.from(s.getClock(), s
				.getMachinesNotBeingUsedByUser(), s.getGridDemand(), s.getNumJobsArrivedInLastTurn());
		add(quadruple);
	}

	@Override
	protected String getReportName() {
		return REPORT_NAME;
	}

	@Override
	public void generateOutput() throws IOException {

	}
}
